﻿/*
COPYRIGHT (C) 2010 EPISERVER AB

THIS FILE IS PART OF PROPERTY PACK FOR EPISERVER CMS.
DYNAMIC CONTENT CONTRIB FOR EPISERVER CMS IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

PROPERTY PACK FOR EPISERVER CMS IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH PROPERTY PACK FOR EPISERVER CMS. IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EPiServer.Core.PropertySettings;
using System.Collections.Generic;
using EPiServer.UI.WebControls;

namespace EPiServer.Research.Properties
{
    
    public class InputDropdownOptions : PropertySettingsControlBase
    {
        protected TextBox valueField;

        protected ListBox optionsList;
        protected ToolButton upBtn;
        protected ToolButton downBtn;
        protected ToolButton deleteBtn;
        protected TextBox newNameBox;
        protected TextBox newValueBox;
        protected ToolButton addBtn;

        protected override void CreateChildControls()
        {
            //Funny stuff: 
            //Make a list and allow people to add to it.

            //label
            Label l = new Label();
            l.Text = "List Items:";
            Controls.Add(l);
            Controls.Add(new LiteralControl("<br/>"));
            optionsList = new ListBox();
            optionsList.Width = new Unit(200);
            Controls.Add(optionsList);
            Controls.Add(new LiteralControl("<br/><br/>"));
            upBtn = new ToolButton();
            upBtn.DisablePageLeaveCheck = true;
            upBtn.Text = "Move Up";
            upBtn.Click += new EventHandler(upBtn_Click);
            Controls.Add(upBtn);
            downBtn = new ToolButton();
            downBtn.Text = "Move Down";
            downBtn.DisablePageLeaveCheck = true;
            downBtn.Click += new EventHandler(downBtn_Click);
            Controls.Add(downBtn);
            deleteBtn = new ToolButton();
            deleteBtn.Text = "Delete";
            deleteBtn.DisablePageLeaveCheck = true;
            deleteBtn.Click += new EventHandler(deleteBtn_Click);
            Controls.Add(deleteBtn);
            Controls.Add(new LiteralControl("<br/><br/>"));
            Label l1 = new Label();
            l1.Text = "Add New Item (Name, Value):";
            Controls.Add(l1);
            Controls.Add(new LiteralControl("<br/>"));
            newNameBox = new TextBox();
            newValueBox = new TextBox();
            Controls.Add(newNameBox);
            Controls.Add(newValueBox);
            addBtn = new ToolButton();
            addBtn.Text = "Add";
            addBtn.DisablePageLeaveCheck = true;
            addBtn.Click += new EventHandler(addBtn_Click);
            Controls.Add(addBtn);

        }

        void addBtn_Click(object sender, EventArgs e)
        {
            optionsList.Items.Add(new ListItem() { Text = newNameBox.Text, Value = newValueBox.Text });
            newNameBox.Text = string.Empty;
            newValueBox.Text = string.Empty;
        }

        void deleteBtn_Click(object sender, EventArgs e)
        {
            if (optionsList.SelectedItem != null)
            {
                optionsList.Items.Remove(optionsList.SelectedItem);
            }
        }

        void downBtn_Click(object sender, EventArgs e)
        {
            if (optionsList.SelectedItem != null)
            {
                int pos = optionsList.SelectedIndex;
                if (pos == (optionsList.Items.Count - 1)) return;
                ListItem li = optionsList.SelectedItem;
                optionsList.Items.Remove(li);
                optionsList.Items.Insert(pos + 1, li);
            }
        }

        void upBtn_Click(object sender, EventArgs e)
        {
            if (optionsList.SelectedItem != null)
            {
                int pos = optionsList.SelectedIndex;
                if (pos == 0) return;
                ListItem li = optionsList.SelectedItem;
                optionsList.Items.Remove(li);
                optionsList.Items.Insert(pos - 1, li);
            }
        }

        public InputDropdownOptions()
        {

        }

        public override void LoadSettingsUI(IPropertySettings settings)
        {
            //Sets values in UI
            EnsureChildControls();
            DropDownValues dv = (DropDownValues)settings;
            for (int i = 0; i < dv.Names.Length; i++)
            {
                ListItem li = new ListItem() { Text=dv.Names[i], Value=dv.Values[i] };
                optionsList.Items.Add(li);
            }
        }

        public override void UpdateSettings(IPropertySettings settings)
        {
            //Updates values
            EnsureChildControls();
            DropDownValues dv = (DropDownValues)settings;
            dv.Names = new string[optionsList.Items.Count];
            dv.Values = new string[optionsList.Items.Count];
            dv.Checked = new bool[optionsList.Items.Count];
            for (int i = 0; i < optionsList.Items.Count; i++)
            {
                dv.Names[i] = optionsList.Items[i].Text;
                dv.Values[i] = optionsList.Items[i].Value;
                dv.Checked[i] = optionsList.Items[i].Selected;
            }
        }

    }
}
